﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Windows.Threading;

namespace bluetooth_Rec
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private SerialPort BluetoothConnection;
        // Declare a 2 bytes vector to store the message length header 
        private Byte[] MessageLength = { 0x00, 0x00 };


        public MainWindow()
        {
            InitializeComponent();
            //readData();
        }

        //// send the message itself 
        //BluetoothConnection.Write(Command, 0, Command.Length); 
        private void initialize()
        {
            BluetoothConnection = new SerialPort();
            BluetoothConnection.PortName = "COM8";
            BluetoothConnection.ReadTimeout = 1500;
            BluetoothConnection.Open();
        }

        public string readData()
        {
            if (BluetoothConnection == null)
                initialize();
            BluetoothConnection.ReceivedBytesThreshold = 12;
            BluetoothConnection.DataReceived += new SerialDataReceivedEventHandler(BluetoothConnection_DataReceived);
            return "";
        }

        void BluetoothConnection_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int packetSize = BluetoothConnection.ReadByte();
            packetSize += ((int)BluetoothConnection.ReadByte()) << 8;
            int commandType = BluetoothConnection.ReadByte(); //should be 0x80 direct command no response needed
            int command = BluetoothConnection.ReadByte(); // 0x09 Message Write
            int mailboxNumber = BluetoothConnection.ReadByte(); // Doesn't apply - could be used for a message queue
            int payloadLength = BluetoothConnection.ReadByte();
            int instructionType = BluetoothConnection.ReadByte(); //0x01, 0x32, 0x33, 0x34, 0x35,0x00
            
            if (instructionType == 0)
            {
                int targetHeading = BluetoothConnection.ReadByte();
                targetHeading += BluetoothConnection.ReadByte();
                int estDistance = BluetoothConnection.ReadByte();
                estDistance += BluetoothConnection.ReadByte();
                int terminationBuffer = BluetoothConnection.ReadByte();

                this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Forms.MethodInvoker(delegate()
                {
                    btData.Text += targetHeading.ToString() + " " + estDistance.ToString() + "\n";

                }));
            }

            else if (instructionType == 1)
            {
                int targetHeading = BluetoothConnection.ReadByte();
                targetHeading += BluetoothConnection.ReadByte(); 
                int estDistance = BluetoothConnection.ReadByte();
                estDistance += BluetoothConnection.ReadByte();
                int x1 = BluetoothConnection.ReadByte();
                x1 += BluetoothConnection.ReadByte();
                int y1 = BluetoothConnection.ReadByte();
                y1 += BluetoothConnection.ReadByte();
                int x2 = BluetoothConnection.ReadByte();
                x2 += BluetoothConnection.ReadByte();
                int y2 = BluetoothConnection.ReadByte();
                y2 += BluetoothConnection.ReadByte();
                int terminationBuffer = BluetoothConnection.ReadByte();

                this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Forms.MethodInvoker(delegate()
                {
                    btData.Text += targetHeading.ToString() + " " + estDistance.ToString() + " " + x1.ToString() + " " + y1.ToString() + " " + x2.ToString() + " " + y2.ToString() + " " + "\n";

                }));
            }

            else if (instructionType == 2)
            {
                int x1 = BluetoothConnection.ReadByte();
                x1 += BluetoothConnection.ReadByte();
                int y1 = BluetoothConnection.ReadByte();
                y1 += BluetoothConnection.ReadByte();
                int x2 = BluetoothConnection.ReadByte();
                x2 += BluetoothConnection.ReadByte();
                int y2 = BluetoothConnection.ReadByte();
                y2 += BluetoothConnection.ReadByte();
                int weight = BluetoothConnection.ReadByte();

                this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Forms.MethodInvoker(delegate()
                {
                    
                }));
            }
        }

        public void closeConnection()
        {
            BluetoothConnection.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            initialize();
            readData();
        }

        private void Button_Click1(object sender, RoutedEventArgs e)
        {
            btData.Text = "";
        }

     
    }
}
